#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <climits>
#include <memory>
#include <algorithm>
#include <numeric>

using namespace std;

// 训练计划I--使奇数位于偶数的前面
class Solution
{
public:
    vector<int> trainingPlan(vector<int> &actions)
    {
        int left = -1, right = 0, n = actions.size();
        while (right < n)
        {
            if (actions[right] % 2 == 1)
            {
                std::swap(actions[++left], actions[right]);
            }
            right++;
        }

        return actions;
    }
};

// 链表求和
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
    {
        ListNode *head = new ListNode(-1);
        ListNode *tail = head;
        int carry = 0;
        while (l1 || l2)
        {
            int sum = 0;
            if (l1)
            {
                sum += l1->val;
                l1 = l1->next;
            }
            if (l2)
            {
                sum += l2->val;
                l2 = l2->next;
            }
            ListNode *node = new ListNode((sum + carry) % 10);
            tail->next = node;
            tail = tail->next;
            carry = (sum + carry) / 10;
        }
        if (carry)
        {
            ListNode *node = new ListNode(carry);
            tail->next = node;
        }
        return head->next;
    }
};

// 整数反转
class Solution
{
public:
    int reverse(int x)
    {
        int sum = 0;
        while (x)
        {
            if (sum < INT_MIN / 10 || sum > INT_MAX / 10)
                return 0;
            int tmp = x % 10;
            sum = sum * 10 + tmp;
            x /= 10;
        }

        return sum;
    }
};

// 不同路径
class Solution
{
public:
    int uniquePaths(int m, int n)
    {
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));

        dp[0][1] = 1;
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];

        return dp[m][n];
    }
};

// 对称二叉树
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
    bool isSametree(TreeNode *root1, TreeNode *root2)
    {
        if (root1 == nullptr && root2 == nullptr)
            return true;
        if (root1 == nullptr || root2 == nullptr)
            return false;
        if (root1->val != root2->val)
            return false;
        return isSametree(root1->left, root2->right) && isSametree(root1->right, root2->left);
    }
    bool isSymmetric(TreeNode *root)
    {
        if (root == nullptr)
            return true;
        return isSametree(root->left, root->right);
    }
};

// 三数之和
class Solution
{
public:
    vector<vector<int>> threeSum(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        vector<vector<int>> ans;
        int n = nums.size();
        for (int i = 0; i < n - 2; i++)
        {
            int target = -nums[i];
            int left = i + 1, right = n - 1;
            while (left < right)
            {
                int sum = nums[left] + nums[right];
                if (sum == target)
                {
                    ans.push_back({nums[i], nums[left], nums[right]});
                    left++;
                    right--;
                    while (left < right && nums[left] == nums[left - 1])
                        left++;
                    while (left < right && nums[right] == nums[right + 1])
                        right--;
                }
                else if (sum < target)
                    left++;
                else
                    right--;
            }
            while (i < n - 3 && nums[i] == nums[i + 1])
                i++;
        }

        return ans;
    }
};

// 缺失的第一个正数
class Solution
{
public:
    int firstMissingPositive(vector<int> &nums)
    {
        int n = nums.size();
        // 将所有的非正数变为n + 1
        for (auto &x : nums)
        {
            if (x <= 0)
                x = n + 1;
        }

        for (int i = 0; i < n; i++)
        {
            int num = abs(nums[i]);
            // 取绝对值之后小于n则说明是正数
            // 我们将其变为负数作为标记
            if (num <= n)
            {
                nums[num - 1] = -abs(nums[num - 1]);
            }
        }

        for (int i = 0; i < n; i++)
        {
            if (nums[i] > 0)
            {
                return i + 1;
            }
        }

        return n + 1;
    }
};